package com.youths.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youths.dao.LabelConsumerMapper;
import com.youths.dao.YouthInfoMapper;
import com.youths.domain.LabelConsumer;
import com.youths.domain.YouthInfo;
import com.youths.service.YouthInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/6/24
 */

@Service
public class YouthInfoServiceImpl implements YouthInfoService {
    @Autowired(required = false)
    YouthInfoMapper youthInfoMapper;

    @Autowired(required = false)
    LabelConsumerMapper labelConsumerMapper;

    @Override
    public int addYouthInfo(String youthCardId, String youthName, Integer youthSex, String youthAddress, String youthPlace, String youthNation, Date youthBirth, Float youthMotherHeight, Float youthFatherHeight, Integer consumerID, Integer state) {
        int result = youthInfoMapper.add(youthCardId, youthName, youthSex, youthAddress, youthPlace, youthNation, youthBirth, youthFatherHeight, youthMotherHeight, consumerID, state);
        return result;
    }

    @Override
    public List<YouthInfo> getInfoList(Integer consumerId) {
        List<YouthInfo> result = youthInfoMapper.list(consumerId);
        return result;
    }

    @Override
    public boolean check(Integer consumerId, Integer youthId) {
        List<YouthInfo> list = youthInfoMapper.check(consumerId, youthId);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int update(Integer youthId, String youthName, String youthAddress, Float youthFatherHeight, Float youthMotherHeight, Integer state) {
        int result = youthInfoMapper.update(youthId, youthName, youthAddress, youthFatherHeight, youthMotherHeight, state);
        return result;
    }

    @Override
    public YouthInfo queryYouthInfo(Integer youthId) {
        return youthInfoMapper.query(youthId);
    }

    @Override
    public PageInfo getYouthInfoList(Integer pageNum,Integer youthId, String name, String card, String account) {
        PageHelper.startPage(pageNum,10);
        List<LabelConsumer> consumers = null;
        if(account != null && !account.equals("")){
            consumers = labelConsumerMapper.getList(null,null,null,account);
            if(consumers.size() == 0){
                return new PageInfo();
            }
        }
        List<YouthInfo> youthInfos = youthInfoMapper.getList(youthId,name,card,consumers);
        PageInfo<YouthInfo> youthInfoPageInfo = new PageInfo<>(youthInfos);
//        System.out.println(youthInfoPageInfo.getList().get(0).getYouthBirth());
        return youthInfoPageInfo;
    }

    @Override
    @Transactional
    public String updateYouthInfo(Integer youthId, String cardId, String name, Integer sex, String address, String place, String nation, Integer fatherHeight, Integer motherHeight, Integer state) {
        try{
            String birth = cardId.substring(6,10)+'-'+cardId.substring(10,12)+'-'+cardId.substring(12,14)+" 00:00:00.0";
            Integer result = youthInfoMapper.updateYouth(youthId,cardId,name,sex,address,place,nation,fatherHeight,motherHeight,state,birth);
            if(result == 1){
                return "success";
            }
            return "fail";
        }catch (Exception e){
            return "repeat";
        }
    }
}
